package com.dongchedi.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dongchedi.service.OrderService;
import com.dongchedi.utils.ResultMessage;
import com.dongchedi.utils.constant.RedisConstantKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author leibidong
 * @description   订单controller层
 * @date 2021/6/24
 */
@Controller
@RequestMapping("api")
@ResponseBody
public class OrderController {
    @Autowired
    OrderService orderService;

    /*
     * @Author: leibidong
     * @Description: 根据订单支付状态进行主订单查询
     * @Date: 2021/6/26 20:15
     * @Param: [customerId, orderStatus, pageNum, pageSize]
     * @Return: java.lang.String
     **/
    @GetMapping("/select/order/status")
    public String myMasterOrder(Integer customerId,Integer orderStatus,Integer pageNum,Integer pageSize){
        JSONArray jsonArray = orderService.searchOrderByStatus(customerId, orderStatus, pageNum, pageSize);
            if (!jsonArray.isEmpty()) {
                return ResultMessage.getJSONArrayString(1, "我的订单查询成功", jsonArray);
            }

            return ResultMessage.getJSONArrayString(0, "我的订单查询失败", jsonArray);
    }
    /*
     * @Author: leibidong
     * @Description: 添加主订单
     * @Date: 2021/6/26 20:15
     * @Param: [customerId, id, colorType, productCnt]
     * @Return: java.lang.String
     **/
    @PostMapping("/add/order")
    public String addMasterOrder(Integer customerId,Integer id,String colorType,Integer productCnt,Integer addressId){
        JSONObject jsonObject =orderService.addOrder(customerId,id,colorType,productCnt,addressId);
        if(!jsonObject.isEmpty()){
            return ResultMessage.getJSONObjectString(1,"生成订单成功",jsonObject);
        }
        return ResultMessage.getJSONObjectString(0,"生成订单失败",jsonObject);
    }
    /*
     * @Author: leibidong
     * @Description: 查询订单详细信息
     * @Date: 2021/6/26 20:16
     * @Param: [customerId, id]
     * @Return: java.lang.String
     **/
    @GetMapping("/select/order/detail")
    public String selectOrderDetail(Integer customerId,Integer id){
        JSONObject jsonObject =orderService.selectOrderDetail(customerId,id);
        if(!jsonObject.isEmpty()){
            return ResultMessage.getJSONObjectString(1,"查询订单详细信息成功",jsonObject);
        }
        return ResultMessage.getJSONObjectString(0,"查询订单详细信息失败",jsonObject);
    }
    /*
     * @Author: leibidong
     * @Description: 支付订单
     * @Date: 2021/6/26 20:16
     * @Param: [customerId, id]
     * @Return: java.lang.String
     **/
    @PostMapping("/order/detail/appay")
    public  String appayOrder(Integer customerId,Integer id){

        JSONObject jsonObject =orderService.appayOrder(customerId,id);
        if(!jsonObject.isEmpty()){
            return ResultMessage.getJSONObjectString(1,"支付成功",jsonObject);
        }
        return ResultMessage.getJSONObjectString(0,"支付失败",jsonObject);
    }
}
